package com.wujilin.okra.fragment;

import static com.wujilin.okra.Okra.FORMATTER_DATE_TIME;
import static com.wujilin.okra.Okra.EXTRA_OBJECTIVE_ID;
import static com.wujilin.okra.Okra.F_CONTENT;
import static com.wujilin.okra.Okra.F_OBJECTIVE_ID;
import static com.wujilin.okra.Okra.F_TIME;
import static com.wujilin.okra.Okra.T_KEY_RESULT;

import org.joda.time.DateTime;

import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.wujilin.okra.R;
import com.wujilin.okra.database.Sql;

/**
 * 
 * @author Belin Wu
 */
public class KeyResultNewFragment extends KeyResultDialogFragment {
	private EditText contentEdit;
	
	public static KeyResultNewFragment newInstance(long objectiveId) {
		Bundle args = new Bundle();
		args.putLong(EXTRA_OBJECTIVE_ID, objectiveId);
		
		KeyResultNewFragment fragment = new KeyResultNewFragment();
		fragment.setArguments(args);
		
		return fragment;
	}
	
	@Override
	public void onClickPositiveButton(DialogInterface dialog, Bundle args) {
		long objectiveId = args.getLong(EXTRA_OBJECTIVE_ID, 0);
		String content = contentEdit.getText().toString();
		String time = DateTime.now().toString(FORMATTER_DATE_TIME);
		Sql.insert(T_KEY_RESULT, F_OBJECTIVE_ID, objectiveId, F_CONTENT, content, F_TIME, time);
	}

	@Override
	public void onCreateDialog(Builder builder) {
		View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_key_result_new, null);
		contentEdit = (EditText) view.findViewById(R.id.key_result_content);
		builder.setView(view).setTitle(R.string.new_key_result);
	}
}
